#include "widget.h"
#include "ui_widget.h"
#include "gradedelegate.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    model = new QStandardItemModel(this);
    QStringList strLists;
    strLists << "姓名" << "学号" << "成绩";
    model->setHorizontalHeaderLabels(strLists);

    // Model数据模型设置
    QStringList names = {"张三", "李四", "王五"};
    QStringList ids = {"2023001", "2023002", "2023003"};
    QStringList grades = {"85", "92", "78"};

    for (int row = 0; row < 3; ++row) {
        model->setItem(row, 0, new QStandardItem(names[row]));
        model->setItem(row, 1, new QStandardItem(ids[row]));
        QStandardItem* gradeItem = new QStandardItem(grades[row]);
        gradeItem->setData(Qt::AlignCenter, Qt::TextAlignmentRole); // 居中对齐
        model->setItem(row, 2, gradeItem);
   }


   //view视图设置model
   ui->tableView->setModel(model);
   ui->tableView->resize(400, 300);

   //设置成绩列委托，提供编辑功能
   GradeDelegate *delegate = new GradeDelegate();
   ui->tableView->setItemDelegateForColumn(2, delegate); // 只为成绩列设置委托
}

Widget::~Widget()
{
    delete ui;
}

